yjx.controller('joinTeamController', ['$rootScope', '$scope', '$timeout', '$fetch', 'modalService', '$state', 'detailService', function ($rootScope, $scope, $timeout, $fetch, modalService, $state, detailService) {

    function getUserInfo() {
        $fetch.fetch(
            '/userInfo/getById',
            {
                params: {
                    userId: $rootScope.inviterUserId
                }
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.item = data.user;
            })

        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    function joinTeam() {
        $fetch.fetch(
            '/group/join',
            {
                params: {
                    no: $rootScope.teamNo
                },
                type: 'post'
            }
        ).then(function (data) {
            joinSuc();
        }).catch(function (e) {
            if (e.code == 2009) {
                joinSuc();
            } else {
                modalService.mainMsgError(e.msg)
            }
        })
    }
    function joinSuc(){
        $rootScope.teamNo = null;
        $rootScope.inviteTeamId = null;
        $rootScope.changeState = null;
        $rootScope.changeStateName = null;
        $timeout(function(){
            modalService.mainMsgOk("您已成功加入该团队")
            detailService.killDetailAll();
            $state.go('index',{reload:true});
        })
      
        // $state.go('team',{teamNo:null},{reload:true});
    }
    $scope.join = function () {
        if ($rootScope.user.telephone) {
            //当前用户是否为小B
            joinTeam()
        } else {
            detailService.openDetail('register');
        }
    }
    getUserInfo();
    $timeout(function () {
        $rootScope.changeState = true;
        $rootScope.changeStateName = 'close';
    }, 2000)

}])